Public NotInheritable Class frmRegions
    Private m_objMyRegions As MyRegions
    Private m_IsAddedList As New List(Of Boolean)

    Public Sub New(ByVal regions As MyRegions)
        ' This call is required by the Windows Form Designer.
        Me.InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_objMyRegions = regions
        For intRegion As Integer = 1 To m_objMyRegions.Count
            m_IsAddedList.Add(False)
        Next
        Me.FillList()
        Me.Translate()
    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Try
            If Me.cboRegions.Text.Length = 0 Then
                m_objMyRegions.Add()
            Else
                '0.9.4
                'm_objMyRegions.Add(Me.cboRegions.SelectedIndex - 1)
                m_objMyRegions.Add(Me.cboRegions.SelectedIndex)
            End If
        Catch ex As MyRegions.RegionException
            MessageBox.Show(ex.Message, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End Try
        m_IsAddedList.Add(True)
        Me.FillList()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If MessageBox.Show(Language.ReadRegions("msgDELETE"), My.Application.Info.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Try
                '0.9.4
                'm_objMyRegions.RemoveAt(Me.lstRegions.SelectedIndex)
                m_objMyRegions.RemoveAt(Me.lstRegions.SelectedIndex + 1)
            Catch ex As MyRegions.RegionException
                MessageBox.Show(ex.Message, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End Try
            m_IsAddedList.RemoveAt(Me.lstRegions.SelectedIndex)
            Me.FillList()
        End If
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Try
            If Me.cboRegions.Text.Length = 0 Then
                '0.9.4
                'm_objMyRegions.Insert(Me.lstRegions.SelectedIndex)
                m_objMyRegions.Insert(Me.lstRegions.SelectedIndex + 1)
            Else
                '0.9.4
                'm_objMyRegions.Insert(Me.lstRegions.SelectedIndex, Me.cboRegions.SelectedIndex - 1)
                m_objMyRegions.Insert(Me.lstRegions.SelectedIndex + 1, Me.cboRegions.SelectedIndex)
            End If
        Catch ex As MyRegions.RegionException
            MessageBox.Show(ex.Message, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End Try
        m_IsAddedList.Insert(Me.lstRegions.SelectedIndex, True)
        Me.FillList()
    End Sub

    Private Sub lstRegions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstRegions.SelectedIndexChanged
        Me.EnableControls()
    End Sub


    Private Sub EnableControls()
        Me.btnInsert.Enabled = (Me.lstRegions.SelectedIndex >= 0)
        Me.btnDelete.Enabled = (Me.lstRegions.SelectedIndex >= 0)
    End Sub

    Private Sub FillList()
        With Me.cboRegions.Items
            .Clear()
            .Add("")
            .AddRange(m_objMyRegions.ItemList.ToArray)
        End With
        With Me.lstRegions.Items
            .Clear()
            .AddRange(m_objMyRegions.ItemList.ToArray)
            For intRegion As Integer = 0 To m_objMyRegions.Count - 1
                If m_IsAddedList.Item(intRegion) Then
                    .Item(intRegion) = .Item(intRegion).ToString + " *"
                End If
            Next
        End With
        Me.EnableControls()
    End Sub

    Private Sub Translate()
        Me.lblCopyValues.Text = Language.ReadRegions("lblCopyValues")
        Me.btnAdd.Text = Language.ReadRegions("btnAdd")
        Me.btnInsert.Text = Language.ReadRegions("btnInsert")
        Me.btnDelete.Text = Language.ReadRegions("btnDelete")
        Me.btnOK.Text = Language.ReadMessages("msgOK")
        Me.btnCancel.Text = Language.ReadMessages("msgCANCEL")
    End Sub

End Class